package com.hy.dao.impl;

import com.hy.bean.PurchaseApplyDetail;
import com.hy.dao.PurchaseApplyDetailDao;
import com.hy.utils.DBManager;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;

public class PurchaseApplyDetailDaoImpl implements PurchaseApplyDetailDao {

    private final QueryRunner queryRunner = new QueryRunner();
    @Override
    public PurchaseApplyDetail queryById(Integer id) {
        Connection connection = null;
        String sql = "select id, purchase_apply_id purchaseApplyId, goods_id goodsId, goods_count goodsCount, market_price marketPrice, trade_price tradePrice, purchase_price purchasePrice, money, required_time requiredTime from purchase_apply_detail where id = ?";
        try {
            connection = DBManager.getConnection();
            return queryRunner.query(connection, sql, new BeanHandler<>(PurchaseApplyDetail.class), id);
        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public List<PurchaseApplyDetail> queryByPurchaseApplyId(Connection connection, Integer purchaseApplyId) throws SQLException {
        String sql = "select id, purchase_apply_id purchaseApplyId, goods_id goodsId, goods_count goodsCount, market_price marketPrice, trade_price tradePrice, purchase_price purchasePrice, money, required_time requiredTime from purchase_apply_detail where purchase_apply_id = ?";
        return queryRunner.query(connection, sql, new BeanListHandler<>(PurchaseApplyDetail.class), purchaseApplyId);
    }
    @Override
    public long count(PurchaseApplyDetail purchaseApplyDetail) {
        return 0;
    }

    @Override
    public int insert(PurchaseApplyDetail purchaseApplyDetail) {
        Connection connection = null;
        String sql = "INSERT INTO purchase_apply_detail values(seq_purchase_apply_detail.nextval,?,?,?,?,?,?,?,to_date(?,'yyyy-mm-dd'))";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            connection = DBManager.getConnection();
            return queryRunner.update(connection,sql,
                    purchaseApplyDetail.getPurchaseApplyId(),
                    purchaseApplyDetail.getGoodsId(),
                    purchaseApplyDetail.getGoodsCount(),
                    purchaseApplyDetail.getMarketPrice(),
                    purchaseApplyDetail.getTradePrice(),
                    purchaseApplyDetail.getPurchasePrice(),
                    purchaseApplyDetail.getMoney(),
                    sdf.format(purchaseApplyDetail.getRequiredTime()));
        }catch (Exception e){
            throw new RuntimeException(e);

        }finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {

                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public int insertBatch(List<PurchaseApplyDetail> entities) {
        return 0;
    }

    @Override
    public int insertOrUpdateBatch(List<PurchaseApplyDetail> entities) {
        return 0;
    }

    @Override
    public int update(PurchaseApplyDetail purchaseApplyDetail) {
        Connection connection = null;
        String sql = "update purchase_apply_detail set purchase_apply_id = ?, goods_id = ?, goods_count = ?, market_price = ?, trade_price = ?, purchase_price = ?, money = ?, required_time = to_date(?,'yyyy-mm-dd') where id = ?";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            connection = DBManager.getConnection();
            return queryRunner.update(connection,sql,purchaseApplyDetail.getPurchaseApplyId(),purchaseApplyDetail.getGoodsId(),purchaseApplyDetail.getGoodsCount(),purchaseApplyDetail.getMarketPrice(),purchaseApplyDetail.getTradePrice(),purchaseApplyDetail.getPurchasePrice(),purchaseApplyDetail.getMoney(),sdf.format(purchaseApplyDetail.getRequiredTime()),purchaseApplyDetail.getId());
        }catch (Exception e){
            throw new RuntimeException(e);

        }finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {

                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public int deleteById(Integer id) {
        Connection connection = null;
        String sql = "delete from purchase_apply_detail where id = ?";
        try {
            connection = DBManager.getConnection();
            return queryRunner.update(connection,sql,id);
        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public int deleteByIds(Connection connection,Integer id) throws SQLException {
        String sql = "delete from purchase_apply_detail where id = ?";
        return queryRunner.update(connection,sql,id);
    }
}
